package com.cckat.controller;

import com.alibaba.fastjson.JSON;
import com.cckat.base.BaseController;
import com.cckat.entity.Role;
import com.cckat.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

/**
 * @author 静海基地
 */
@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {

    @DubboReference
    private RoleService roleService;


    @RequestMapping
    public String findPage(Model model, @RequestParam Map<String, Object> filters) {
        //调用service处理业务逻辑
        PageInfo<Role> page = roleService.findPage(filters);
        //将filters和page共享到请求域
        model.addAttribute("filters", filters);
        model.addAttribute("page", page);
        return "role/index";
    }

    public String findAll(Model model) {
        //调用service处理业务逻辑
        List<Role> list = roleService.findAll();
        //将所有的角色信息共享在请求域中
        model.addAttribute("list", list);
        //跳转到role/index.html
        return "role/index";
    }

    @RequestMapping("/create")
    public String create() {
        return "role/create";
    }

    @RequestMapping("/save")
    public String save(Role role, Model model) {
        roleService.insert(role);
        model.addAttribute("messagePage", "新增角色成功");
        //跳转到success界面并由html中调用js实现关闭窗口并返回列表并刷新功能
        return "common/successPage";
    }

    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id, Model model) {
        Role role = roleService.getById(id);
        model.addAttribute("role", role);
        return "role/edit";
    }

    @RequestMapping("/update")
    public String update(Role role, Model model) {
        roleService.update(role);
        //return successPage("修改用户成功",model);
        model.addAttribute("messagePage", "修改角色成功");
        return "common/successPage";
    }

    @RequestMapping("delete/{id}")
    public String delete(@PathVariable Long id) {
        roleService.delete(id);
        return "redirect:/role";
    }

    @RequestMapping("/assignShow/{roleId}")
    public String assignShow(@PathVariable("roleId") Long roleId, Model model) {
        model.addAttribute("roleId", roleId);
        List<Map<String, Object>> PermissionList = roleService.findZnodes(roleId);
        String zNodes = JSON.toJSONString(PermissionList);
        model.addAttribute("zNodes", zNodes);
        return "role/assignShow";
    }

    @RequestMapping("/assignPermission")
    public String assignPermission(Long roleId, @RequestParam("permissionIds") List<Long> permissionIds, Model model){
        roleService.assignPermission(roleId, permissionIds);
        return successPage("分配权限成功", model);
    }
}